using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace MES
{
    public partial class ReplicationForm : Form
    {
        private int count = 1;
        private static ReplicationForm instance = null;
        private ReplicationForm()
        {
            InitializeComponent();
        }
        public static ReplicationForm createInstance()
        {
            if (instance == null)
            {
                instance = new ReplicationForm();
            }
            return instance;
        }

        private void RepTimer_Tick(object sender, EventArgs e)
        {
            RepTimer.Enabled = false;
            Application.DoEvents();
            lbRep.Items.Add("Invoke times " + count.ToString() + " begin at " + DateTime.Now.ToLocalTime().ToString());
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            SqlConnection conn = new SqlConnection(config.ConnectionStrings.ConnectionStrings["MES.Properties.Settings.wjspc"].ToString());            
            SqlCommand cmd = new SqlCommand("EXEC wjspc.M2.Synchronize_Records", conn);
            try
            {
                conn.Open();                    
                cmd.ExecuteNonQuery();
                if (lbRep.Items.Count > 1000)
                    lbRep.Items.Clear();
                lbRep.Items.Add("Invoke times " + count.ToString() + " end at " + DateTime.Now.ToLocalTime().ToString());
                count += 1;
            }
            catch (Exception exc)
            {
                lbRep.Items.Add(exc.Message.ToString());
                RepTimer.Interval = 1000;
                RepTimer.Enabled = true;
                return;
                
            }
            finally
            {
                conn.Close();               
            }
            RepTimer.Interval = 5000;
            RepTimer.Enabled = true;
        }
    }
}